<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户新增</title>
    <script>
        window.onload = () => {
            // 给获取的 id 添加点击事件
            // 添加用户
            document.getElementById("btn_submit").onclick = function () {
                let username = document.getElementById("username").value;
				console.log(username);
				let email = document.getElementById("email").value;
                let tel = document.getElementById("tel").value;
                let tbody = document.getElementById("userTbody");
                if (username !== null && username !== '' && username !== undefined) {
                    // 设置要插入的表格行信息
                    let tr = "<tr id=\"" + new Date().getTime() + "\">\n" +
                        "\t\t\t\t<td>" + username + "</td>\n" +
                        "\t\t\t\t<td>" + email + "</td>\n" +
                        "\t\t\t\t<td>" + tel + "</td>\n" +
                        "\t\t\t\t<td><a href=\"javascript:delRow('" + new Date().getTime() + "')\">删除</a></td>\n" +
                        "\t\t\t</tr>";

                    // 把新插入的行信息插入表格中
                    console.log(tbody.innerText);
                    console.log(tbody.innerHTML);
                    tbody.innerHTML += tr;
                } else {
					alert("姓名不能为空！");
				}
				// 添加信息之后把表单设置为空
				document.getElementById("username").value = "";
				document.getElementById("email").value = "";
				document.getElementById("tel").value = "";
            };

            document.getElementById("btn_removeAll").onclick = () => {
                var tbody = document.getElementById("userTbody");
                tbody.innerHTML = "";
            };
        };
        // 删除用户
        function delRow(id) {
            let tr = document.getElementById(id);
            tr.parentNode.removeChild(tr);
        }
    </script>
</head>
<body style="background-color: #ecf0f5;font-family: 微软雅黑;color: #475059;min-width: 1000px;overflow: auto">
	<div id="myDiv" style="height: 200px;width: 200px;position: fixed;top: 500;right: 50;
		background:;">
	        <span id="show"></span>
	        <input type="button" id="box" value="关闭" />
	    </div>
	
	    <script type="text/javascript">
		
	        function $(sel) {
				var bigImg = document.createElement("img");     //创建一个img元素  
				        bigImg.src="http://www.baidu.com/img/baidu_sylogo1.gif";   //给img元素的src属性赋值  
				        //bigImg.width="320";  //320个像素 不用加px  
				        var myDiv = document.getElementById('myDiv'); //获得dom对象  
				        myDiv.appendChild(bigImg); 
	            //querySelector() 
	            //返回文档中匹配指定 CSS 选择器的一个元素。
	            //获取文档中的 sel 选择器中的元素
	            return document.querySelector(sel);
	        }
	        //广告弹出自动关闭
	        //返回带有指定标签名的对象的集合  输出div标签内的内容 
	        var box = document.getElementsByTagName('div')[0];
	        console.log(box);
	        //onclick of null/undefined  
	        //返回文档中 id="#show"的元素  并赋给oSpan
	        var oSpan = $('#show');
	        //定义count初始值为5
	        var count = 5;
	        //改变oSpan中的值为count  
	        //即id为#show   span标签中的内容
	        //为什么在setInterval()的里边外边都要获取count？
	        //在外边获取count值 count的值才会从初始化的值开始显示
	        //若只写函数里边的那部分，
	        //则第一次显示的count值是执行过count--后的值
	        oSpan.innerHTML = count;
	        //定义一个计时器timer 传函数 函数执行的间隔时间
	        //倒计时  count--
	        var timer = setInterval(function() {
	            count--;
	            //在计时器里获取count值 count的只才可以--
	            oSpan.innerHTML = count;
	            //判断 当count减到0时 让广告自动关闭并清除定时器 
	            if (count == 0) {
	                box.style.display = 'none';
	                clearInterval(timer)
	            }
	        }, 1000)
	
	        //获取按钮
	        let btn = document.getElementById('box')
	            //点击按钮时 让广告自动关闭并清除定时器
	        btn.onclick = function() {
	            console.log(1)
	            box.style.display = 'none';
	            clearInterval(timer)
	        }
	    </script>
		
<form name="userForm">
    <center><div>
        用户信息管理<br/>
        用户名:<input id="username" name="username" type="text" size=15/>
        E-mail:<input id="email" name="email" type="text" size=15/>
        电话:<input id="tel" name="tel" type="text" size=15/>
        <input type="button" value="添加" id="btn_submit"/>
        <input type="button" value="删除所有" id="btn_removeAll"/>
		</div>
    </center>
</form>
<hr/>
<div class="container">
<table id="backViewTable" class="table table-hover table-sm table2excel"border="1" cellpadding=0 cellspacing=0 width=1000 align="left" align="center"  align="right" >
    <thead>
    <tr>
        <th>用户名</th>
        <th>E-mail</th>
        <th>电话</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="userTbody">
    <tr id="tr1">
        <td>小肥羊</td>
        <td>wujizhang@163.com</td>
        <td>18212345678</td>
        <td><a href="javascript:delRow('tr1')">删除</a></td>
    </tr>
	<tr id="tr2">
	    <td>小肥羊</td>
	    <td>wujizhang@163.com</td>
	    <td>18212345678</td>
	    <td><a href="javascript:delRow('tr1')">删除</a></td>
	</tr>
	<tr id="tr3">
	    <td>小肥羊</td>
	    <td>wujizhang@163.com</td>
	    <td>18212345678</td>
	    <td><a href="javascript:delRow('tr1')">删除</a></td>
	</tr>
	<tr id="tr4">
	    <td>小肥羊</td>
	    <td>wujizhang@163.com</td>
	    <td>18212345678</td>
	    <td><a href="javascript:delRow('tr1')">删除</a></td>
	</tr>
	<tr id="tr5">
	    <td>小肥羊</td>
	    <td>wujizhang@163.com</td>
	    <td>18212345678</td>
	    <td><a href="javascript:delRow('tr1')">删除</a></td>
	</tr>
	<tr id="tr6">
	    <td>小肥羊</td>
	    <td>wujizhang@163.com</td>
	    <td>18212345678</td>
	    <td><a href="javascript:delRow('tr1')">删除</a></td>
	</tr>
    </tbody>
</table>
</div>
<button class="btn btn-primary btn-sm" onclick="tablesToExcel(['backViewTable'], ['ProductDay1'], 'TestBook.xls', 'Excel')">Export to Excel</button>
<script>
    var tablesToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
            , tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
            + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'
            + '<Styles>'
            + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'
            + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'
            + '</Styles>'
            + '{worksheets}</Workbook>'
            , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
            , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'
            , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
        return function(tables, wsnames, wbname, appname) {
            var ctx = "";
            var workbookXML = "";
            var worksheetsXML = "";
            var rowsXML = "";

            for (var i = 0; i < tables.length; i++) {
                if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);

//              控制要导出的行数
                for (var j = 0; j < tables[i].rows.length; j++) {
                    rowsXML += '<Row>';

//                    控制导出的列数（在本例中，最后一列为button,导出的文件会出错，所以导出到倒数第二列
                    for (var k = 0; k < tables[i].rows[j].cells.length-1; k++) {
                        var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
                        var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
                        var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
                        dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
                        var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
                        dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null;
                        ctx = {  attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':''
                            , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String'
                            , data: (dataFormula)?'':dataValue
                            , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':''
                        };
                        rowsXML += format(tmplCellXML, ctx);
                    }
                    rowsXML += '</Row>'
                }
                ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i};
                worksheetsXML += format(tmplWorksheetXML, ctx);
                rowsXML = "";
            }

            ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
            workbookXML = format(tmplWorkbookXML, ctx);

//          查看后台的打印输出
            console.log(workbookXML);

            var link = document.createElement("A");
            link.href = uri + base64(workbookXML);
            link.download = wbname || 'Workbook.xls';
            link.target = '_blank';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    })();
    </script>


</body>
</html>
